﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CustomControls.Dialog;
using CustomControls.InheritedFrameworkControls;
using CustomControls.MiscControls;
using CustomControls.PhotoViewer;
using CustomControls.PictureControl;
using CustomControls.Shop;
using Infrasturcture.Global.Helpers.CustomHelpers.Events;
using Infrasturcture.TouchLibrary;
using MallInteractiveMap.ClientDataHandler;
using MallInteractiveMap.Common;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;
using TouchControls;
using TouchFramework;

namespace MallInteractiveMap
{
    public partial class MainWindow
    {
        private ShopInformation _shopInformation;
        private PhotoStack _photoStack;
        private DialogViewer _photoStackDialog;
        private bool _skipPictureDetails;
        private List<ImageSource> _currentDisplayedPictures = new List<ImageSource>();
        private List<ClosePictureControl> _currentDisplayedPictureControls = new List<ClosePictureControl>();

        private void PopulatePhotos()
        {
            WaitScreen.Show();
            _photoStack = new PhotoStack();
            FrameworkManager.RegisterElement((IMTouchControl)_photoStack.PhotoLeftButton, false, new[] { TouchAction.Tap });
            FrameworkManager.RegisterElement((IMTouchControl)_photoStack.PhotoRightButton, false, new[] { TouchAction.Tap });
            FrameworkManager.RegisterElement((IMTouchControl)_photoStack.MainPhotoView, false, new[] { TouchAction.Tap });
            _photoStack.Closed += PhotoStackClosed;
            _photoStack.PopulatePhotos(GetShopPhotos(_shopInformation.ShopData));

            _photoStack.MainPhotoView.MouseLeftButtonDown += MainPhotoView_MouseLeftButtonDown;
            _photoStack.AddRemovedPhotos += PhotoStackAddRemovedPhotos;

            foreach (UIElement item in _photoStack.PhotoCollection)
            {
                var photo = item as ThumbnailPhoto;
                FrameworkManager.RegisterElement((IMTouchControl)photo.PhotoButton, false, new[] { TouchAction.Tap });
            }
            _photoStackDialog = ShowDialog(_photoStack, new SolidColorBrush(Colors.White));
            _photoStackDialog.Closed += PhotoStackDialogClosed;
            WaitScreen.Hide();
        }

        void PhotoStackDialogClosed(object sender, EventArgs e)
        {
            _skipPictureDetails = false;
        }

        void PhotoStackAddRemovedPhotos(object sender, AddRemovePhotoEvntArgs e)
        {
            FrameworkManager.RegisterElement((IMTouchControl)e.AddedItem, false, new[] { TouchAction.Tap });
            FrameworkManager.UnRegisterElement(e.RemovedItem);

        }

        void MainPhotoView_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var imageSource = _photoStack.MainPhotoView.Source;
            if (_currentDisplayedPictures.Contains(imageSource)) return;

            WaitScreen.Show();
            var pictureControl = new ClosePictureControl();
            var i = pictureControl.SetImage(imageSource);
            _currentDisplayedPictures.Add(imageSource);
            _currentDisplayedPictureControls.Add(pictureControl);

            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.HighQuality);
            FrameworkManager.RegisterElement((IMTouchControl)pictureControl, true, null);
            pictureControl.CloseButton.Tag = FrameworkManager.RegisterElement(pictureControl.CloseButton, false, new[] { TouchAction.Tap });
            var zIndex = Canvas.GetZIndex(_photoStackDialog);
            Canvas.SetZIndex(pictureControl, zIndex + 1);
            pictureControl.CloseClick += PictureControlCloseClick;
            canvas.Children.Add(pictureControl);
            WaitScreen.Hide();
        }

        void PictureControlCloseClick(object sender, EventArgs e)
        {
            var pictureControl = (ClosePictureControl)sender;
            RemovePicture(pictureControl);
        }

        private void RemovePicture(ClosePictureControl pictureControl)
        {
            _currentDisplayedPictures.Remove(pictureControl.ImageSource);
            _currentDisplayedPictureControls.Remove(pictureControl);
            FrameworkManager.UnRegisterElement(((IMTContainer)pictureControl.CloseButton.Tag).Id);
            FrameworkManager.RemoveControl(pictureControl);
        }

        private void ShowShopInformation(DTOShop shop, MapElement mapElement)
        {
            WaitScreen.Show();
            if (CurrentFloor == null || CurrentFloor.Id != shop.FloorId)
            {
                var error = string.Empty;
                var param = new QueryParamList
                                {
                                    new QueryParamObj {ParamName = "Id", DBType = DbType.Int32, ParamValue = shop.FloorId}
                                };
                var floor = ClientData.GetFloor(param, ref error).FirstOrDefault();
                CheckFloorButton(floor);
            }
            _shopInformation = new ShopInformation { ShopData = shop, ShopNumber = shop.ShopNo, Tag = mapElement };
            _shopInformation.SeePictureButton.Click += SeePictureButton_Click;
            _shopInformation.Closed += ShopInformationClosed;
            FrameworkManager.RegisterElement((IMTouchControl)_shopInformation.SeePictureButton, false, new[] { TouchAction.Tap });
            ShowDialog(_shopInformation);
            WaitScreen.Hide();
        }

        void SeePictureButton_Click(object sender, RoutedEventArgs e)
        {
            if (_skipPictureDetails) return;
            _skipPictureDetails = true;
            PopulatePhotos();
        }

        #region Click Event
        void ShopInformationClosed(object sender, DataEventArgs e)
        {
            if(_currentDisplayedPictures.Count>0)
                foreach (var closePictureControl in _currentDisplayedPictureControls.ToArray())
                {
                    RemovePicture(closePictureControl);
                }

            if(_photoStack!=null)
            {
                _photoStack.OnClosed(new DataEventArgs());
            }

            FrameworkManager.UnRegisterElement(_shopInformation.SeePictureButton);
            if (_shopInformation.Tag is MapElement)
                ((MapElement)_shopInformation.Tag).DeActivate();
        }

        void PhotoStackClosed(object sender, DataEventArgs e)
        {
            foreach (UIElement item in _photoStack.PhotoCollection)
            {
                var photo = item as ThumbnailPhoto;
                FrameworkManager.UnRegisterElement(photo.PhotoButton);
            }
            FrameworkManager.UnRegisterElement(_photoStack.PhotoLeftButton);
            FrameworkManager.UnRegisterElement(_photoStack.PhotoRightButton);
            FrameworkManager.UnRegisterElement(_photoStack.MainPhotoView);
        }


        #endregion

        #region Data Helper
        public DTOShop GetShop(string shopNo)
        {
            var error = string.Empty;
            var param = new QueryParamList
                            {
                                new QueryParamObj()
                                    {
                                        ParamName = "ShopNo", DBType = DbType.String, ParamValue = shopNo
                                    },
                                    new QueryParamObj()
                                    {
                                        ParamName = "FloorId", DBType = DbType.Int32, ParamValue = CurrentFloor.Id
                                    }
                            };
            var shop = ClientData.GetShop(param, ref error);
            if (!string.IsNullOrEmpty(error))
            {

            }
            return shop.FirstOrDefault();
        }
        public List<DTOShopPhotos> GetShopPhotos(DTOShop shop)
        {
            var error = string.Empty;
            var photos = ClientData.GetShopPhoto(shop.ShopNo, shop.FloorId, ref error);
            if (!string.IsNullOrEmpty(error))
            {

            }
            return photos;
        }
        #endregion
    }
}
